package org.proxycine.gestsalles.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.proxycine.gestsalles.model.entities.Salle;

public class SalleDao extends Adapter {
	
	private static SalleDao instance;
	
	private SalleDao() {
		instance = this;
	}
	
	public static SalleDao getInstance() {
		if(instance == null)
			instance = new SalleDao();
		
		return instance;
	}
	
	public Salle getSalleFromId(int id)
	{
		Salle salle = null;
		String request = "SELECT * " +
				"FROM Salle S " +
				"WHERE S.id = '" + id + "'";
				
		try {
			ResultSet rs = getStatement().executeQuery(request);
					
			if (rs.next()) {
				salle = new Salle(rs.getInt("id"),
						rs.getInt("numero_salle"),
						rs.getInt("nombre_place"),
						CinemaDao.getInstance().getCinema(rs.getString("cinema")));
			}
			
			rs.close();
					
			} catch (SQLException e) {
				e.printStackTrace();
			}		
		
		return salle;
	}
	
	public List<Salle> getScreensFromACinema(String login)
	{
		List<Salle> salles = new ArrayList<Salle>();
		String request = "SELECT S.* " +
		"FROM Salle S, Cinema C, Employe E " +
		"WHERE C.id = S.cinema " +
		"AND E.login = C.gerant " +
		"AND E.login = '" + login + "' " +
		"ORDER BY S.numero_salle";
		
		try {
			ResultSet rs = getStatement().executeQuery(request);
			
			while (rs.next()) {
				salles.add(new Salle(rs.getInt("id"),
						rs.getInt("numero_salle"),
						rs.getInt("nombre_place"),
						CinemaDao.getInstance().getCinema(rs.getString("cinema"))));
			}
			
			rs.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return salles;
		}
	
	public Salle getSalleFromLogin(String loginGerant, int numero_salle)
	{
		Salle salle = null;
		String request = "SELECT S.* " +
				"FROM Salle S, Cinema C, Employe E " +
				"WHERE C.id = S.cinema " +
				"AND E.login = C.gerant " +
				"AND E.login = '" + loginGerant + "' " +
				"AND S.numero_salle = '" + numero_salle +"' " + 
				"ORDER BY S.numero_salle";
				
				try {
					ResultSet rs = getStatement().executeQuery(request);
					
					if (rs.next()) {
						salle = new Salle(rs.getInt("id"),
								rs.getInt("numero_salle"),
								rs.getInt("nombre_place"),
								CinemaDao.getInstance().getCinema(rs.getString("cinema")));
					}
					
					rs.close();
					
				} catch (SQLException e) {
					e.printStackTrace();
				}		
		return salle;
		
	}

}
